package com.example.muyonghui.mytimetable;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by muyonghui on 16/12/19.
 */
public class CourseListAdapter extends ArrayAdapter<Courses> {
    private int resourceid;

    public CourseListAdapter(Context context, int textViewResourceId, List<Courses> objects){
        super(context, textViewResourceId, objects);
        resourceid = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Courses courses = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceid, null);
            viewHolder = new ViewHolder();
            viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkbox);
            viewHolder.courseName = (TextView) view.findViewById(R.id.course_name);
//            viewHolder.courseTeacher = (TextView) view.findViewById(R.id.course_teacher);
            viewHolder.courseTime = (TextView) view.findViewById(R.id.course_time);
            viewHolder.courseAddress = (TextView) view.findViewById(R.id.course_address);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.courseName.setText(courses.getCourseName());
//        viewHolder.courseTeacher.setText(courses.getTeacherName());
        viewHolder.courseTime.setText(courses.getTime());
        viewHolder.courseAddress.setText(courses.getAddress());
        viewHolder.checkBox.setChecked(false);
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean bool) {
                courses.setCheckBox(bool);
            }
        });

//        if(position%2 != 0){
//            view.setBackgroundColor(Color.argb(50, 0, 0, 200));
//        }
        return view;

    }

    class ViewHolder {
        TextView courseName;
//        TextView courseTeacher;
        TextView courseTime;
        TextView courseAddress;
        CheckBox checkBox;
    }
}
